<script type="text/javascript">
    $(document).ready(function() {

        $('#petition').dataTable();

    });

    function verifyPetition(id) {
        var task = "verify";
        //alert(id);

        $.post('processPetition.php', {task: task, petition_id: id}, function(data) {

            //alert(data);
            window.location.href = "index.php?pages=finance";
        });

    }

    function cancelPetition(id) {
        var task = "cancel";
        //alert(id);

//        $('<div>').dialog2({
//            autoOpen: true,
//            title: "ยกเลิกฎีกา",
//            content: "dlgCancel_peti.php?petition_id=" + id,
//            id: "modal-cancelPeti"
//        });

        if (confirm("คุณแน่ใจใช่ไหมที่ต้องการ ยกเลิก สัญญานี้")) {

            $('<div>').dialog2({
                autoOpen: true,
                title: "ยกเลิกฎีกา",
                content: "dlg_cancel.php?tblname=tbl_petition&id=" + id,
                id: "modal-cancel"
            });
        }
    }

    function approvedPetition(id) {
        var task = "approved";

        $('<div>').dialog2({
            autoOpen: true,
            title: "อนุมัติ",
            content: "dlg_date_approved_petition.php?id=" + id + "&task=" + task,
            id: "modal-date"
        });
        
//        var task = "approved";
//
//        $('<div>').dialog2({
//            autoOpen: true,
//            title: "อนุมัติ",
//            content: "dlgDate.php?petition_id=" + id + "&task=" + task,
//            id: "modal-dlgDate"
//        });
        /*
         $.post('processPetition.php',{task: task, petition_id: id}, function(data) {
         window.location.href="index.php?pages=finance";
         });
         
         */

    }
    function disapprovalPetition(id) {
        var task = "disapproval";

        $.post('processPetition.php', {task: task, petition_id: id}, function(data) {
            window.location.href = "index.php?pages=finance";
        });

    }

</script>

<?php
//echo "finance.php"
require_once("includes/connect.php");

$db = new myDBS;
$db->selectDB("account");
//$where = "petition_id = '$petition_id'";
$sql = "SELECT tbl_petition.*, pp_check_job.proID, projectName, amount, instalment "
        . " FROM tbl_petition, pp_check_job "
        . " WHERE tbl_petition.ppID = pp_check_job.ppID";
$result = $db->runSQL($sql);
?>
<table id="petition" class="table table-hover table-bordered" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>#</th>
            <th>เลขที่ฎีกา</th>
            <th>เลขที่โครงการ</th>
            <th>ชื่อโครงการ</th>
            <th>งวดที่</th>
            <th>ยอดเบิกจ่าย</th>
            <!--<th>รายละเอียดเพิ่มเติม</th>-->
            <th>ตรวจรับฏีกา</th>
            <th>อนุมัติ</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 0;
        while ($rows = $db->fetch_object($result)) {
            $i++;
            ?>
            <tr>
                <td><?php echo $i; ?></td>
                <td><p class="text-center"><?php echo $rows->p_id; ?></p></td>
                <td><?php echo $rows->proID; ?></td>
                <td><?php echo $rows->projectName; ?></td>
                <td><?php echo $rows->instalment; ?></td>
                <td><p class="text-right"><?php echo number_format($rows->inpNet); ?></p></td>
    <!--                <td>
                    <a href="#" id="info_<?php echo $rows->ppID; ?>"><i class="icon-list"></i>&nbsp;ดู</a>
                </td>-->
                <td>
                    <?php
                    if ($rows->veri_status == "รอตรวจสอบ" && $rows->status == "") {

                        echo "<a href='javascript:verifyPetition(\"$rows->petition_id\")' data-toggle='tooltip' title='ผ่าน'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:cancelPetition(\"$rows->petition_id\")' data-toggle='tooltip' title='ไม่ผ่าน'><i class='icon-remove'></i></a>";
//                        echo "<a href='javascript:verifyPetition(\"$rows->id\")' data-toggle='tooltip' title='ผ่าน'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:cancel(\"$rows->id\")' data-toggle='tooltip' title='ยกเลิก'><i class='icon-remove'></i></a>";
                    } elseif ($rows->status == "ยกเลิก") {
                        echo $rows->status;
                    } else {
                        echo $rows->veri_status;
                    }
                    ?>

                </td>
                <td>
                    <?php
                    if ($rows->veri_status == "ผ่าน") {
                        if ($rows->approved_status == "รออนุมัติ") {

                            echo "<a href='javascript:approvedPetition(\"$rows->petition_id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-thumbs-up'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->petition_id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-thumbs-down'></i></a>";
//                            echo "<a href='javascript:approvedPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-remove'></i></a>";
                        } else {
                            echo $rows->approved_status;
                        }
                    } else {
                        echo "รอการตรวจสอบ";
                    }
                    ?>
                </td>
            </tr>
            <?php
        }
        ?>

    </tbody>


</table>
